package com.google.code.timeeditapplication.controller.implementation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import android.os.Environment;

import com.google.code.timeeditapplication.model.implementation.ICal2Schedule;
import com.google.code.timeeditapplication.model.implementation.Schedule;

/**
 * Tools for schedule
 * 
 * @author Jennifer
 * 
 */
public class ScheduleTools {

	// private constructor since this is a tool class hence it will not be
	// instantiated
	private ScheduleTools() {

	}

	/**
	 * Hides a visible lesson
	 */
	public static void hideLesson() {
		ScheduleTools._hideSchedule();
	}

	/**
	 * Shows hidden schedules
	 */
	public static void showHiddenSchedule() {
		ScheduleTools._showHiddenSchedule();
	}

	/**
	 * Merges schedules if there are several schedules
	 */
	public static void mergeSchedules() {
		ScheduleTools._mergeSchedules();
	}

	private static void _hideSchedule() {

	}

	private static void _mergeSchedules() {

	}

	private static void _showHiddenSchedule() {

	}

	/**
	 * Configures which schedule to use for "My Schedule"
	 * 
	 * @param name
	 *            Name of Schedule to be saved as mine (filename of icalendar
	 *            file)
	 */
	public static void saveAsMySchedule(String name) {
		try {
			FileWriter writer = new FileWriter(new File(
					Environment.getExternalStorageDirectory() + "/tea",
					"my_schedule"));
			writer.write(name);
			writer.close();
		} catch (IOException e) {
			// shouldn't happen =)
		}
	}

	/**
	 * Returns "My Schedule"
	 * 
	 * @return Schedule set as mine by saveAsMySchedule method
	 */
	public static Schedule getMySchedule() {
		try {
			return ICal2Schedule.getInstance().fileToSchedule(
					ScheduleTools.getMyScheduleLocation(), "My Schedule");
		} catch (FileNotFoundException e) {
			// shouldn't happen since the file should be saved earlier
		}
		return null;
	}

	// reads configuration file and returns which Schedule is set as mine.
	private static String getMyScheduleLocation() {
		String location = Environment.getExternalStorageDirectory() + "/tea/";
		try {
			Scanner sc = new Scanner(new FileInputStream(
					Environment.getExternalStorageDirectory()
							+ "/tea/my_schedule"));
			location += sc.next();
		} catch (FileNotFoundException e) {
			// shouldn't happen =)
		}
		return location.trim();
	}
}
